package com.yunc.modules.shop.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.utils.R;
import com.yunc.modules.shop.entity.Item;
import com.yunc.modules.shop.entity.ItemSpecs;
import com.yunc.modules.shop.entity.dto.ItemParamDTO;
import com.yunc.modules.shop.service.IItemService;
import com.yunc.modules.shop.service.IItemSpecsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品管理
 *
 * @author congcong tao
 * @date 2018/12/24 9:29
 */
@RestController
@RequestMapping("item")
public class ItemController extends CommonController {

    private static final Logger logger = LogManager.getLogger(ItemController.class);

    @Autowired
    private IItemService itemService;
    @Autowired
    private IItemSpecsService itemSpecsService;
    
    @GetMapping("/page")
    public R page(Page<Item> page, Item item){
        itemService.page(page, new QueryWrapper<>(item));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(Item item){
        QueryWrapper<Item> ew = new QueryWrapper<>();
        List<Item> list = itemService.list(ew);
        return R.ok().put("list", list).put("item", item);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        Item item = itemService.getById(id);
        // 取出params数据
        String paramData = item.getParamData();
        List<ItemParamDTO> itemParamList = new ArrayList<>();
        if (StringUtils.isNotEmpty(paramData)) {
            itemParamList = JSON.parseArray(paramData, ItemParamDTO.class);
        }
        // 查询itemSpecs
        QueryWrapper<ItemSpecs> ew = new QueryWrapper<>();
        ew.eq("item_id", id);
        List<ItemSpecs> itemSpecsList = itemSpecsService.list(ew);
        return R.ok().put("item", item).put("itemParamList", itemParamList).put("itemSpecsList", itemSpecsList);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody Item item){
        item.setCreateTime(new Date());
        boolean flag = itemService.save(item);
        if (flag) {
            Long id = item.getId();
            if (StringUtils.isNotEmpty(item.getItemSpecsJson())) {
                itemSpecsService.saveSpecs(id, item.getItemSpecsJson());
            }
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    public R update(@RequestBody Item item){
        boolean flag = itemService.updateById(item);
        if (flag) {
            Long id = item.getId();
            if (StringUtils.isNotEmpty(item.getItemSpecsJson())) {
                itemSpecsService.saveSpecs(id, item.getItemSpecsJson());
            }
            return R.ok();
        }
        return R.error();
    }
}
